package multimedia;

import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;

import midlet.VadeMecumMIDlet;
import ui.VadeMecumImageViewer;

public class SaveSnapshotThread implements Runnable {

	private VadeMecumMIDlet midlet;
	
	private byte[] picture;
	
	private Alert alert;
	
	private VadeMecumImageViewer previous;
	
	private long fileId;
	
	public SaveSnapshotThread(VadeMecumMIDlet midlet, VadeMecumImageViewer previous, byte[] picture, long fileId) {
		this.midlet = midlet;
		this.picture = picture;
		this.previous = previous;
		this.fileId = fileId;
		alert = new Alert("", "Picture saved!", null, AlertType.INFO);
		alert.setTimeout(1500);
	}
	
	public void run() {
		System.out.println("vai salvar");
		try { 
			String url = "file:///e:/Images/vm_picture" + fileId + ".jpg";  
			FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);  
			if (!fconn.exists()) {  
				fconn.create();  
			}  
			OutputStream ops = fconn.openOutputStream();  
			ops.write(picture);  
			ops.close();  
			fconn.close();  
			previous.repaint();
			midlet.display.setCurrent(alert, previous);
		} catch (IOException ioe) {  
			midlet.error(ioe);
		} catch (SecurityException se) {
			midlet.error(se);  
		} catch (Exception e) {
			midlet.error(e);
		}
	}

}
